//
//  JXButton.m
//  JXButtonDemo
//
//  Created by pconline on 2016/11/28.
//  Copyright © 2016年 pconline. All rights reserved.
//

#import "JXButton.h"

@implementation JXButton

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self=[super initWithCoder:aDecoder]) {
        [self commonInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit{
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    self.titleLabel.textColor = [UIColor blackColor];
    self.titleLabel.adjustsFontSizeToFitWidth  = YES;
    
//    if (_labLmage) {
//
//
//
//    }
}
- (void)setLabLmage:(UIImage *)labLmage{
    if (_labLmage != labLmage) {
        _labLmage = labLmage;
    }
    CGRect labTextFrame =  [self.titleLabel textRectForBounds:self.titleLabel.frame limitedToNumberOfLines:1];
    UIImageView * imageV = [[UIImageView alloc] initWithImage:_labLmage];
    imageV.frame = CGRectMake(CGRectGetMaxX(labTextFrame)+2, 12, 12, 12);
    
    [self.titleLabel addSubview:imageV];
}
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleX = 0;
    CGFloat titleY = contentRect.size.height *0.60;
    CGFloat titleW = contentRect.size.width;
    CGFloat titleH = contentRect.size.height - titleY;
    return CGRectMake(titleX, titleY, titleW, titleH);
}
- (void)setImageReact:(CGFloat)imageReact{
    if (_imageReact!=imageReact) {
        _imageReact = imageReact;
    }
}

- (void)setImageW:(CGFloat)imageW{
    if (_imageW!=imageW) {
        _imageW = imageW;
    }
}
- (void)setLabStr:(NSString *)labStr{
    if (_labStr!=labStr) {
        _labStr = labStr;
    }
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self setTitle:_labStr forState:UIControlStateNormal];
}
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat imageW = 45;
    CGFloat imageH = 45;
    if (_imageW) {
        imageW = _imageW;
    }
    CGFloat x = contentRect.size.width/2-20;
    
    if (_imageReact>0) {
        imageW = _imageReact;
        imageH = _imageReact;
        x = contentRect.size.width/2-_imageReact/2;
    }
//    else{
//        _imageReact = 55;
//        imageW = _imageReact;
//        imageH = _imageReact;
//        x = contentRect.size.width/2-_imageReact/2;
//    }
    
    
    return CGRectMake(x, 0, imageW, imageH);
}

@end
